package adapter;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import lfm.mpr.R;
import objects.Chat;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AdapterChat extends ArrayAdapter<Chat>{
	
	private ArrayList<Chat> list_chat;
	private Context context;
	private SharedPreferences prefs;
	private String user;
	
	public AdapterChat(Context context, ArrayList<Chat> list) {
		super(context, R.layout.chat_message, list);
		// TODO Auto-generated constructor stub
		this.list_chat = list;
		this.context = context;
		prefs = context.getSharedPreferences("MisPreferencias",Context.MODE_PRIVATE);
        user = prefs.getString("user", "lfm");
	}//end_AdapterChat
	
	static class ViewHolder {
        protected TextView leftmessage, rightmessage, user, leftime, righttime ;
        protected ImageView img;
    }
	
	
	
	


	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
        final ViewHolder viewHolder;
        
        if (convertView == null) {
        	
        	viewHolder = new ViewHolder();
        	LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        	
	        convertView  = inflater.inflate(R.layout.chat_message, null);
	            
	        viewHolder.leftmessage = (TextView) convertView.findViewById(R.id.leftmessage);
	        viewHolder.rightmessage = (TextView) convertView.findViewById(R.id.rightmessage);
	       
	        
	       
            convertView.setTag(viewHolder);

        } else {
        	viewHolder = (ViewHolder) convertView.getTag();
        }
        
        //Log.i("LFM_Log","REFRESCANDO ADAPTER");
        Drawable background_send = context.getResources().getDrawable(android.R.drawable.editbox_background);
        
        
        if(this.list_chat.get(position).isResponse()){
        	viewHolder.leftmessage.setText(this.list_chat.get(position).getMessage());
        	if(this.list_chat.get(position).getUser().getUser().equals(user)){
        		viewHolder.leftmessage.setTextColor(Color.parseColor("#0000FF"));
        	}
        	else {
        		viewHolder.leftmessage.setTextColor(Color.parseColor("#FF4500"));
        	}
            viewHolder.leftmessage.setBackgroundDrawable(background_send);
            viewHolder.rightmessage.setText("");
            viewHolder.rightmessage.setBackgroundDrawable(null);
        }
        else {
        	viewHolder.leftmessage.setText("");
            viewHolder.leftmessage.setBackgroundDrawable(null);
            viewHolder.rightmessage.setText(this.list_chat.get(position).getMessage());
            viewHolder.rightmessage.setTextColor(Color.parseColor("#0000FF"));
            viewHolder.rightmessage.setBackgroundDrawable(background_send);
        }
        
        
        
        
        
        return convertView;
        }
	
	

	

}//end_AdapterChat
